from flask import Blueprint, session,  request, render_template
from flask_cors import CORS
from models import Knn, db_session
from .utils.common_util import email_send, build_response
import json

knn_bp = Blueprint('knn', __name__)
CORS(knn_bp)


@knn_bp.route('/Knn', methods=['GET', 'POST'])
def knn():
    if request.method == 'POST':
        print(1)
        json_dict = json.loads(request.get_data(as_text=True))
        title = json_dict.get('title')
        knn_record = db_session.query(Knn).filter_by(title=title).all()
        code = "200"
        print(knn_record)
        score = knn_record[0].score
        if not knn_record:
            data = {"msg": "账号不存在"}
            response = build_response(data, code)
            return response
        data = {"score": score}
        response = build_response(data, code)
        return response
